//
//  TShareEventHandler.m
//  Traceability
//
//  Created by ZhaoDongBo on 16/8/11.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "TShareEventHandler.h"
#import "UINavigationBar+BackgroundColor.h"

@implementation TShareEventHandler

+ (void)setupShareSDK:(NSDictionary *)launchOptions {
    
    [ShareSDK registerApp:@"183b0a2ec76d0"
          activePlatforms:@[
                            @(SSDKPlatformTypeSMS),
                            @(SSDKPlatformTypeMail),
                            @(SSDKPlatformTypeFacebook),
                            @(SSDKPlatformTypeTwitter)]
                 onImport:^(SSDKPlatformType platformType) {
                     
                 }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
              
              switch (platformType)
              {
                  case SSDKPlatformTypeFacebook:
                      [appInfo SSDKSetupFacebookByApiKey:@"1760402294214989"
                                               appSecret:@"bac36000dc0da40c9bec2a392168a07c"
                                                authType:SSDKAuthTypeBoth];
                      break;
                  case SSDKPlatformTypeTwitter:
                      [appInfo SSDKSetupTwitterByConsumerKey:@"gZg525UpiDK4ITcwC7UEg18Ov"
                                              consumerSecret:@"jQaqZDLXkcHKFymy4I3Ank2BcdGN5b6YVbnodxbFqEfAVZQ6gp"
                                                 redirectUri:@"http://en.xbniao.com"];
                      break;
                  default:
                      break;
              }
          }];
}


+ (void)shareWithShareURL:(NSString *)shareURL
            shareImageUrl:(NSArray*)shareImageUrlArray
               shareTitle:(NSString *)shareTitle
                shareText:(NSString *)shareText
                shareType:(SSDKPlatformType)shareType
                  topView:(UIView *)topView
               stateBlock:(void (^)(SSDKResponseState))stateBlock
{
    //创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
//    shareURL = shareURL?:[NSString stringWithFormat:@"%@/customer/dist/r.html",APIBaseURL];
    shareURL = @"https://goo.gl/JuDQYU";
    shareTitle = shareTitle?:LZLocalizedString(@"share_app_title", nil);
    shareText = shareText?:LZLocalizedString(@"share_app_content", nil);
    
    //分享图片
    if (shareImageUrlArray == nil || shareImageUrlArray.count == 0) {
        shareImageUrlArray = @[[UIImage imageNamed:@"share_app_image_en"]];
    }
    
    NSLog(@"%@",shareImageUrlArray);
    
    // 根据类型设置分享内容
    switch (shareType) {
        case SSDKPlatformTypeSMS:
        {
            shareImageUrlArray = nil;
            shareText = [shareText stringByAppendingString:shareURL];
        }
            break;
        case SSDKPlatformTypeMail:
        {
            shareImageUrlArray = nil;
            shareText = [shareText stringByAppendingString:shareURL];
        }
            break;
        case SSDKPlatformTypeFacebook:
        {
            
        }
            break;
        case SSDKPlatformTypeTwitter:
        {
            NSInteger urlLength = shareURL.length;
            if (shareText.length>(140-urlLength)) {
                shareText = [[shareText substringWithRange:NSMakeRange(0, (140-urlLength-6))] stringByAppendingString:[NSString stringWithFormat:@"...%@",shareURL]];
            }
            else {
                shareText = [shareText stringByAppendingString:shareURL];
            }
        }
            break;
            
            
        default:
            break;
    }
    
    //分享内容
    [shareParams SSDKSetupShareParamsByText:shareText
                                     images:shareImageUrlArray
                                        url:[NSURL URLWithString:shareURL]
                                      title:shareTitle
                                       type:SSDKContentTypeAuto];
    
    [shareParams SSDKEnableUseClientShare];
    
    //直接分享
    [ShareSDK share:shareType
         parameters:shareParams
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error)
     {
         
         DLog(@"%@", userData);
         
         if (stateBlock) {
             stateBlock(state);
         }
         switch (state) {
             case SSDKResponseStateBegin: {
                 DLog(@"Begin");
                 break;
             }
             case SSDKResponseStateSuccess: {
                 DLog(@"Success");
                 break;
             }
             case SSDKResponseStateFail: {
                 if (shareType == SSDKPlatformTypeMail) {
                     [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];
                 }
                 DLog(@"Fail %@", [NSString stringWithFormat:@"%@", error]);
                 break;
             }
             case SSDKResponseStateCancel: {
                 DLog(@"Cancel");
                 break;
             }
             default:
                 break;
         }
     }];
    
    [SSDKAuthViewStyle setCancelButtonLabelColor:UIColorFromRGB(0x666666, 1)];
    [SSDKAuthViewStyle setTitle:@"Authorize"];
    [SSDKAuthViewStyle setTitleColor:UIColorFromRGB(0x333333, 1)];
    [SSDKAuthViewStyle setStatusBarStyle:UIStatusBarStyleDefault];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

+ (void)jumpToAppstoreForMerchantApp {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/us/app/laziz!-m/id1192631415?l=zh&ls=1&mt=8"]];
}

+ (BOOL)isMobileGuoJi:(NSString *)mobileNumbel {
    
    NSString *aaa = @"^\\s*\\+?\\s*(\\(\\s*\\d+\\s*\\)|\\d+)(\\s*-?\\s*(\\(\\s*\\d+\\s*\\)|\\s*\\d+\\s*))*\\s*$";
    
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", aaa];
    if (([regextestct evaluateWithObject:mobileNumbel]
         )) {
        return YES;
    }
    return NO;
}

@end
